//@author A0096566M
#ifndef LINEEDIT_H
#define LINEEDIT_H

#include <QTextEdit>

#include <QDebug>
#include <QLineEdit>
#include <QStringList>
#include <QStringListModel>
#include <QString>
#include <QCompleter>

#include <QAbstractItemView>
#include <QKeyEvent>

#include <QStyledItemDelegate>
#include <QPainter>
#include <QTextDocument>
#include <QAbstractTextDocumentLayout>
#include <QApplication>

#include "Taak.h"

class HtmlDelegate : public QStyledItemDelegate
{
protected:
    void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
};

class MyCompleter : public QCompleter
{
    Q_OBJECT

public:
    Taak* taak;
    int index;
    int selectedIndex;


    std::vector<string> inputHistory;
    int historyIndex;
    QObject* mLineEdit;

    inline MyCompleter(const QStringList& words, QObject * parent) :
            QCompleter(parent), m_list(words), m_model()
    {
        setModel(&m_model);
        index = -1;
        selectedIndex = -1;
        historyIndex = -1;
    }

    bool MyCompleter::eventFilter(QObject *obj, QEvent *event);

    inline void update(QString word)
    {
        // Do any filtering you like.
        // Here we just include all items that contain word.
        //QStringList filtered = m_list.filter(word, caseSensitivity());
        //Set feedback
        QStringList filtered;
        vector<string> list = taak->getFeedbackDisplay();
        for(int i=0 ; i<list.size(); i++){
            filtered.append(QString::fromStdString(list[i]));
        }

        m_model.setStringList(filtered);
        m_word = word;

        //
        HtmlDelegate* delegate = new HtmlDelegate();
        //qDebug()<< m_model.rowCount();
        for(int i=0 ; i<m_model.rowCount();i++)
            this->popup()->setItemDelegateForRow(i,delegate);

        complete();
    }

    inline QString word()
    {
        return m_word;
    }

private:
    QStringList m_list;
    QStringListModel m_model;
    QString m_word;
};

class MyLineEdit : public QTextEdit
{
    Q_OBJECT

public:
    MyLineEdit(QWidget *parent = 0);
    ~MyLineEdit();

    void setCompleter(MyCompleter *c);
    MyCompleter *completer() const;


protected:
    void keyPressEvent(QKeyEvent *e);
    bool eventFilter(QObject *obj, QEvent *event);

signals:
    void returnPressed();


private slots:
    void insertCompletion(const QString &completion);

private:
    MyCompleter *c;
};



#endif // LINEEDIT_H
